package com.example.easeimpro.controller.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.example.easeimpro.R;
import com.example.easeimpro.model.bean.Model;
import com.example.easeimpro.model.bean.UserInfo;
import com.example.easeimpro.model.dao.UserAccountDao;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

public class LoginActivity extends BaseActivity {
    private EditText mUserEt;
    private EditText mPwdEt;
    private LinearLayout loginItem;
    private Button mApply;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mUserEt = findViewById(R.id.et_user);
        mPwdEt = findViewById(R.id.et_pwd);
        mApply = findViewById(R.id.btn_apply);
        loginItem = findViewById(R.id.loginItem);
        TranslateAnimation animation = new TranslateAnimation(0,0,800, 0);
        animation.setDuration(400);
       loginItem.startAnimation(animation);

        mApply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onLogin();
            }
        });
    }

    public void onLogin(){
        //获取输入值 并去除前后空格
        String user = mUserEt.getText().toString().trim();
        String pwd = mPwdEt.getText().toString().trim();
        if(user.equals("")||user.length()<=0){
            showToast("请输入用户名");
        }
        if(pwd.equals("")||pwd.length()<=0){
            showToast("请输入密码");
        }
        Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                EMClient.getInstance().login(user, pwd, new EMCallBack() {
                    @Override
                    public void onSuccess() {
                        Model.getInstance().LoginSuccess(new UserInfo(user));
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
//                                EMClient.getInstance().groupManager().loadAllGroups();
//                                EMClient.getInstance().chatManager().loadAllConversations();
                                Model.getInstance().getAccountDbDao(mContext).addAccount(new UserInfo(user));
                                showToast("登录成功");
                                //查詢本地用戶數據庫 用戶是否已經登錄
                                UserInfo account = new UserAccountDao(mContext).getAccountByHxId(EMClient.getInstance().getCurrentUser());

                                if(account.getHxId() != null){

                                    IntentNextActivity(MainActivity.class);
                                }
                            }
                        });

                    }

                    @Override
                    public void onError(int code, String error) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                showToast("用户不存在"+error);
                                if(error.equals("Same User is already login")){
                                    showToast("用户已登录"+error);
                                    EMClient.getInstance().logout(true, new EMCallBack() {
                                        @Override
                                        public void onSuccess() {

                                        }

                                        @Override
                                        public void onError(int code, String error) {

                                        }
                                    });
                                }
                            }
                        });

                    }
                });
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}